package com.example.localalbum.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.example.localalbum.common.LocalImageHelper;
import com.example.localalbum.common.LocalImageHelper.LocalFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class UrlImageScrollView
        extends RecyclerView {

    private Context context;
    private List<LocalFile> pictures = new ArrayList<>();
    private List<String> names = new ArrayList<>();

    public UrlImageScrollView(Context context) {
        this(context,null);
    }

    public UrlImageScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public void addFiles(List<String> urls) {
        for (String url : urls) {
            LocalFile localFile = new LocalFile();
            localFile.setOriginalUri(url);
            localFile.setThumbnailUri(url);
            pictures.add(localFile);
        }
        getAdapter().notifyDataSetChanged();
        LocalImageHelper.getInstance().setCurrentSize(pictures.size());
    }

    public void addFiles(List<String> urls, List<String> tags) {
        names.addAll(tags);
        for (String url : urls) {
            LocalFile localFile = new LocalFile();
            localFile.setOriginalUri(url);
            localFile.setThumbnailUri(url);
            pictures.add(localFile);
        }
        getAdapter().notifyDataSetChanged();
        LocalImageHelper.getInstance().setCurrentSize(pictures.size());
    }

    public List<LocalFile> getFiles(){
        return pictures;
    }

    private void init() {
        setLayoutManager(new GridLayoutManager(context,1, LinearLayoutManager.HORIZONTAL,false));
        ImageAdapter imageAdapter = new ImageAdapter(context,pictures,names);
        imageAdapter.setNeverShow(true);
        imageAdapter.setOnItemClickListener(new ImageAdapter.OnItemClickListener() {
            @Override
            public void onClick(int pos) {
                showViewPager(pos);
            }
        });
        imageAdapter.setOnDeleteListener(new ImageAdapter.OnDeleteListener() {
            @Override
            public void onDelete(int pos) {
                if (onDeleteListener != null) {
                    onDeleteListener.onDelete(pos);
                }
            }
        });
        setAdapter(imageAdapter);
        if(!isInEditMode()) {
            LocalImageHelper.getInstance().setCurrentSize(pictures.size());
        }
    }

    private void showViewPager(int index) {
        Intent intent = new Intent(context, PagerView.class);
        intent.putExtra("FILE_LIST", (Serializable) pictures);
        intent.putExtra("INDEX", index);
        context.startActivity(intent);
    }

    public OnDeleteListener onDeleteListener;

    public void setOnDeleteListener(OnDeleteListener onDeleteListener){
        this.onDeleteListener = onDeleteListener;
    }

    public interface OnDeleteListener{
        void onDelete(int pos);
    }
}
